package com.zing.service.impl;

import com.zing.context.BaseContext;
import com.zing.dto.ShoppingCartDTO;
import com.zing.entity.Dish;
import com.zing.entity.Setmeal;
import com.zing.entity.ShoppingCart;
import com.zing.exception.BaseException;
import com.zing.mapper.DishMapper;
import com.zing.mapper.SetmealMapper;
import com.zing.mapper.ShoppingCartMapper;
import com.zing.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前商品是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId()); //用户自己的购物车数据
        List<ShoppingCart> shoppingCartList= shoppingCartMapper.list(shoppingCart);

        if(shoppingCartList!= null && shoppingCartList.size() > 0) {  //如果已经存在,数量加1,再更新
             shoppingCart = shoppingCartList.get(0);
             shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);

        } else { //如果不存在,插入一条数据,数量为1
            Long setmealId = shoppingCart.getSetmealId();
            if(setmealId != null) {  //添加套餐到购物车
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            } else { //添加菜品到购物车
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public void reduceShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //查询当前商品在购物车表中数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId()); //用户自己的购物车数据
        List<ShoppingCart> shoppingCartList= shoppingCartMapper.list(shoppingCart);

        if(shoppingCartList == null || shoppingCartList.size() == 0) { // 如果没有商品数据,抛出异常
            throw new BaseException("购物车中减少商品错误: 商品不存在");
        }

        //如果有商品数据
        shoppingCart = shoppingCartList.get(0);
        Integer number = shoppingCart.getNumber();
        Long setmealId = shoppingCart.getSetmealId();
        if(number == 1) { //删除商品
            shoppingCartMapper.deleteById(shoppingCart.getId());
        } else { // 减少商品数量
            shoppingCart.setNumber(number - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void cleanByUserId() {
        shoppingCartMapper.cleanByUserId(BaseContext.getCurrentId());
    }
}
